package com.ruoyi.ucar.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.obs.services.model.AccessControlList;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.ucar.boreq.*;
import com.ruoyi.ucar.boresp.*;
import com.ruoyi.ucar.common.ResultEnum;
import com.ruoyi.ucar.domain.*;
import com.ruoyi.ucar.domain.po.CustomerDetailMobileQueryPo;
import com.ruoyi.ucar.domain.po.CustomerDetailQueryPo;
import com.ruoyi.ucar.domain.po.CustomerListQueryPo;
import com.ruoyi.ucar.domain.po.CustomerSignQueryPo;
import com.ruoyi.ucar.mapper.*;
import com.ruoyi.ucar.moreq.*;
import com.ruoyi.ucar.service.ISysFileMainService;
import com.ruoyi.ucar.service.ITCustomerService;
import com.ruoyi.ucar.service.ITOrderPayDetailService;
import com.ruoyi.ucar.service.ITOrderService;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.util.NanoTimestampUtil;
import com.ruoyi.ucar.voreq.CustomerAddVoReq;
import com.ruoyi.ucar.voreq.CustomerUpdateVoReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户主Service业务层处理
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Service
public class TCustomerServiceImpl  extends ServiceImpl<TCustomerMapper, TCustomer>   implements ITCustomerService
{
    @Autowired
    private TCustomerMapper tCustomerMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysFileMainService sysFileMainService;

    @Autowired
    private ITOrderPayDetailService itOrderPayDetailService;

    @Autowired
    ITOrderService itOrderService;

    @Autowired
    private TRechargeRecordMapper tRechargeRecordMapper;

    @Autowired
    private TWithdrawRecordMapper tWithdrawRecordMapper;

    @Autowired
    private TConsumeDataMapper tConsumeDataMapper;

    @Autowired
    private   TOrderPayDetailMapper tOrderPayDetailMapper;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public TCustomer selectTCustomerByUserName(String userName) {
        return tCustomerMapper.selectOne(new LambdaQueryWrapper<TCustomer>().eq(TCustomer::getCustomerPhone,userName).eq(TCustomer::getDeleteFlag,1L));
    }

    /**
     * 查询客户主
     * 
     * @param id 客户主主键
     * @return 客户主
     */
    @Override
    public TCustomer selectTCustomerById(Long id)
    {
        return tCustomerMapper.selectTCustomerById(id);
    }

    /**
     * 查询客户主列表
     * 
     * @param tCustomer 客户主
     * @return 客户主
     */
    @Override
    public List<TCustomer> selectTCustomerList(TCustomer tCustomer)
    {
        return tCustomerMapper.selectTCustomerList(tCustomer);
    }

    /**
     * 新增客户主
     * 
     * @param tCustomer 客户主
     * @return 结果
     */
    @Override
    public int insertTCustomer(TCustomer tCustomer)
    {
        return tCustomerMapper.insertTCustomer(tCustomer);
    }

    /**
     * 修改客户主
     * 
     * @param tCustomer 客户主
     * @return 结果
     */
    @Override
    public int updateTCustomer(TCustomer tCustomer)
    {
        return tCustomerMapper.updateTCustomer(tCustomer);
    }

    /**
     * 批量删除客户主
     * 
     * @param ids 需要删除的客户主主键
     * @return 结果
     */
    @Override
    public int deleteTCustomerByIds(Long[] ids)
    {
        return tCustomerMapper.deleteTCustomerByIds(ids);
    }

    /**
     * 删除客户主信息
     * 
     * @param id 客户主主键
     * @return 结果
     */
    @Override
    public int deleteTCustomerById(Long id)
    {
        return tCustomerMapper.deleteTCustomerById(id);
    }

    @Override
    public CustomerListQueryBoResp queryCustomerByCond(CustomerListQueryBoReq customerListQueryBoReq) {
        CustomerListQueryMoReq customerListQueryMoReq= CommonUtil.objToObj(customerListQueryBoReq,CustomerListQueryMoReq.class);
        customerListQueryMoReq.setDeleteFlag(1L);
        Page<CustomerListQueryPo> page=new Page<>(customerListQueryBoReq.getPageIndex(),customerListQueryBoReq.getPageSize());
        IPage<CustomerListQueryPo> customerListQueryPoIPage= tCustomerMapper.customerListQueryPage(page,customerListQueryMoReq);
        if(StringUtils.isNotNull(customerListQueryPoIPage)&&StringUtils.isNotNull(customerListQueryPoIPage.getRecords())&&customerListQueryPoIPage.getRecords().size()>0){
            CustomerListQueryBoResp customerListQueryBoResp=new CustomerListQueryBoResp();
            customerListQueryBoResp.setTotalCount(new Integer(String.valueOf(customerListQueryPoIPage.getTotal())));
            customerListQueryBoResp.setTotalPage(new Integer(String.valueOf(customerListQueryPoIPage.getPages())));
            List<CustomerListQueryBo> customerListQueryBos=new ArrayList<>();
            customerListQueryPoIPage.getRecords().forEach(customerListQueryPo -> {
                CustomerListQueryBo customerListQueryBo=CommonUtil.objToObj(customerListQueryPo,CustomerListQueryBo.class);
                customerListQueryBo.setCustomerTypeDict(DictUtils.getDictLabel("custom_type",String.valueOf(customerListQueryPo.getCustomerType())));
                customerListQueryBo.setRewardFlagDict(DictUtils.getDictLabel("reward_flag",String.valueOf(customerListQueryPo.getRewardFlag())));
                customerListQueryBo.setAgentGradeDict(DictUtils.getDictLabel("agent_grade",String.valueOf(customerListQueryPo.getAgentGrade())));
                customerListQueryBo.setParentAgentGradeDict(DictUtils.getDictLabel("agent_grade",String.valueOf(customerListQueryPo.getParentAgentGrade())));
                customerListQueryBos.add(customerListQueryBo);
            });
            customerListQueryBoResp.setCustomerListQueryBos(customerListQueryBos);
            return customerListQueryBoResp;
        }else{
            return null;
        }
    }

    @Override
    public CustomerListQueryNoPageBoResp   queryCustomerNoPageByCond(CustomerListQueryNoPageBoReq customerListQueryNoPageBoReq) {
        CustomerListQueryNoPageMoReq customerListQueryNoPageMoReq=CommonUtil.objToObj(customerListQueryNoPageBoReq,CustomerListQueryNoPageMoReq.class);
        customerListQueryNoPageMoReq.setDeleteFlag(1L);
        List<CustomerListQueryPo> customerListQueryPos=tCustomerMapper.customerListQueryNoPage(customerListQueryNoPageMoReq);
        if(StringUtils.isNotNull(customerListQueryPos)&&customerListQueryPos.size()>0){
            CustomerListQueryNoPageBoResp customerListQueryNoPageBoResp=new CustomerListQueryNoPageBoResp();
            List<CustomerListQueryNoPageBo> customerListQueryNoPageBos=new ArrayList<>();
            customerListQueryPos.forEach(customerListQueryPo -> {
                CustomerListQueryNoPageBo customerListQueryNoPageBo=CommonUtil.objToObj(customerListQueryPo,CustomerListQueryNoPageBo.class);
                customerListQueryNoPageBo.setCustomerTypeDict(DictUtils.getDictLabel("custom_type",String.valueOf(customerListQueryPo.getCustomerType())));
                customerListQueryNoPageBo.setRewardFlagDict(DictUtils.getDictLabel("reward_flag",String.valueOf(customerListQueryPo.getRewardFlag())));
                customerListQueryNoPageBo.setAgentGradeDict(DictUtils.getDictLabel("agent_grade",String.valueOf(customerListQueryPo.getAgentGrade())));
                customerListQueryNoPageBos.add(customerListQueryNoPageBo);
            });
            customerListQueryNoPageBoResp.setCustomerListQueryNoPageBos(customerListQueryNoPageBos);
            return customerListQueryNoPageBoResp;
        }else{
            return null;
        }
    }
    @Transactional
    @Override
    public String saveCustomer(CustomerAddVoReq customerAddVoReq) {
        TCustomer tCustomer=CommonUtil.objToObj(customerAddVoReq,TCustomer.class);
        if("null".equals(tCustomer.getParentCustomerUuid())||StringUtils.isEmpty(tCustomer.getParentCustomerUuid())||"first_agent".equals(tCustomer.getAgentGrade())){
            tCustomer.setParentCustomerUuid(null);
        }
        if("first_agent".equals(tCustomer.getAgentGrade())){
            tCustomer.setParentCustomerUuid(null);
        }


        String customerUuid=CommonUtil.createUuid();
        SysUser sysUser= userService.selectUserByUserName(customerAddVoReq.getCustomerPhone());
        if(StringUtils.isNull(sysUser)) {
            sysUser = new SysUser();
            sysUser.setUserName(customerAddVoReq.getCustomerPhone());
            sysUser.setNickName(customerAddVoReq.getCustomerPhone());
            sysUser.setPhonenumber(customerAddVoReq.getCustomerPhone());
            userService.saveSysUser(sysUser);
        }
        tCustomer.setSysUserId(sysUser.getUserId());
        //代理商生成分享码
        if(StringUtils.isNotEmpty(customerAddVoReq.getCustomerType())&&"agentCus".equals(customerAddVoReq.getCustomerType())){
            tCustomer.setShareCode(NanoTimestampUtil.getNanoTimestamp());
        }else{
            if(StringUtils.isNotEmpty(customerAddVoReq.getCustomerType())){
                tCustomer.setCustomerType("commonCus");
            }
        }
        tCustomer.setCustomerUuid(customerUuid);
        tCustomer.setDeleteFlag(1L);
        tCustomer.setCreatedBy(SecurityUtils.getUsername());
        tCustomerMapper.insert(tCustomer);
        return customerUuid;
    }




    @Transactional
    @Override
    public int updateCustomer(CustomerUpdateVoReq customerUpdateVoReq) {
        TCustomer tCustomer=this.selectCustomerByCustomerUuid(customerUpdateVoReq.getCustomerUuid());
         if(tCustomerMapper.selectCount(new LambdaQueryWrapper<TCustomer>().eq(TCustomer::getCustomerPhone,customerUpdateVoReq.getCustomerPhone()).eq(TCustomer::getDeleteFlag,1l).ne(TCustomer::getId,tCustomer.getId()))>0){
             return 8;
         };
        //普通客户-一级代理商下级的被代理客户清空
        if(StringUtils.isNotEmpty(customerUpdateVoReq.getCustomerType())&&"commonCus".equals(customerUpdateVoReq.getCustomerType())&&"first_agent".equals(tCustomer.getAgentGrade())){
            this.tCustomerMapper.updateAgentTCustomer2(customerUpdateVoReq.getCustomerUuid());
            this.tCustomerMapper.updateAgentTCustomer(customerUpdateVoReq.getCustomerUuid());
            tCustomer.setShareCode("");
        }

        //普通客户-二级代理商下级的被代理客户清空
        if(StringUtils.isNotEmpty(customerUpdateVoReq.getCustomerType())&&"commonCus".equals(customerUpdateVoReq.getCustomerType())&&"second_agent".equals(tCustomer.getAgentGrade())){
            this.tCustomerMapper.updateAgentTCustomer(customerUpdateVoReq.getCustomerUuid());
            tCustomer.setShareCode("");
        }
        if(StringUtils.isNotEmpty(customerUpdateVoReq.getCustomerType())&&"agentCus".equals(tCustomer.getCustomerType())){
              if(StringUtils.isNotNull(customerUpdateVoReq.getTotalAgencyFee())&&tCustomer.getTotalAgencyFee().compareTo(customerUpdateVoReq.getTotalAgencyFee())>0){
                  TWithdrawRecord tWithdrawRecord=new TWithdrawRecord();
                  tWithdrawRecord.setCustomerUuid(tCustomer.getCustomerUuid());
                  tWithdrawRecord.setDeleteFlag(1l);
                  tWithdrawRecord.setWithdrawMoney(tCustomer.getTotalAgencyFee().subtract(customerUpdateVoReq.getTotalAgencyFee()));
                  tWithdrawRecord.setWithdrawDatetime(LocalDateTimeUtil.now());
                  tWithdrawRecord.setOldBalance(tCustomer.getTotalAgencyFee());
                  tWithdrawRecord.setNowBalance(customerUpdateVoReq.getTotalAgencyFee());
                  tWithdrawRecord.setWithdrawWay("office_withdraw");
                  tWithdrawRecord.setWithdrawRecordUuid(CommonUtil.createUuid());
                  tWithdrawRecord.setWithdrawStatus("toAccount");
                  tWithdrawRecordMapper.insert(tWithdrawRecord);
              }
        }
        tCustomer.setAgentGrade(customerUpdateVoReq.getAgentGrade());
        tCustomer.setReturnMoney(customerUpdateVoReq.getReturnMoney());
        tCustomer.setBalanceNumber(customerUpdateVoReq.getBalanceNumber());
        tCustomer.setCustomerName(customerUpdateVoReq.getCustomerName());
        tCustomer.setCustomerPhone(customerUpdateVoReq.getCustomerPhone());
        tCustomer.setCustomerType(customerUpdateVoReq.getCustomerType());
        tCustomer.setCustomerWechat(customerUpdateVoReq.getCustomerWechat());
        tCustomer.setGiftMoney(customerUpdateVoReq.getGiftMoney());
        if("null".equals(customerUpdateVoReq.getParentCustomerUuid())||StringUtils.isEmpty(customerUpdateVoReq.getParentCustomerUuid())||"first_agent".equals(customerUpdateVoReq.getAgentGrade())){
            tCustomer.setParentCustomerUuid(null);
        }else{
            tCustomer.setParentCustomerUuid(customerUpdateVoReq.getParentCustomerUuid());
        }
        tCustomer.setRemark(customerUpdateVoReq.getRemark());
        tCustomer.setRewardFlag(customerUpdateVoReq.getRewardFlag());
        tCustomer.setTotalAgencyFee(customerUpdateVoReq.getTotalAgencyFee());
        return tCustomerMapper.updateById(tCustomer);
    }

    @Override
    public TCustomer selectCustomerByCustomerUuid(String customerUuid) {
        return this.tCustomerMapper.selectOne(new LambdaQueryWrapper<TCustomer>().eq(TCustomer::getCustomerUuid,customerUuid).eq(TCustomer::getDeleteFlag,1l));
    }

    @Override
    public int deleteTCustomerById(String customerUuid, Long id) {
        TCustomer  tCustomer=null;
        if(StringUtils.isNotEmpty(customerUuid)) {
            tCustomer = this.selectCustomerByCustomerUuid(customerUuid);
            tCustomer.setDeleteFlag(-1l);
            tCustomer.setUpdatedBy(SecurityUtils.getUsername());
            return this.tCustomerMapper.update(tCustomer,new LambdaQueryWrapper<TCustomer>().eq(TCustomer::getCustomerUuid, customerUuid));
        }else{
            tCustomer = this.getById(id);
            tCustomer.setDeleteFlag(-1l);
            tCustomer.setUpdatedBy(SecurityUtils.getUsername());
            return this.tCustomerMapper.update(tCustomer,new LambdaQueryWrapper<TCustomer>().eq(TCustomer::getId, id));
        }
    }

    @Override
    public int batchDelete(String[] customerUuids, Long[] ids) {
        if(StringUtils.isNotNull(customerUuids)&&customerUuids.length>0) {
            return this.tCustomerMapper.deleteTCustomerByUuids(-1l, customerUuids);
        }else{
            return this.tCustomerMapper.deleteTCustomerByids(-1l, ids);
        }
    }

    @Override
    public CustomerDetailQueryBoResp customerDetailQuery(CustomerDetailQueryBoReq customerDetailQueryBoReq) {
        CustomerDetailQueryMoReq  customerDetailQueryMoReq=CommonUtil.objToObj(customerDetailQueryBoReq, CustomerDetailQueryMoReq.class)  ;
        customerDetailQueryMoReq.setDeleteFlag(1l);
        List<CustomerDetailQueryPo> customerDetailQueryPos=this.tCustomerMapper.customerDetailQuery(customerDetailQueryMoReq);
        if(StringUtils.isNotNull(customerDetailQueryPos)&&customerDetailQueryPos.size()>0){
            CustomerDetailQueryPo customerDetailQueryPo=customerDetailQueryPos.get(0);
            CustomerDetailQueryBoResp customerDetailQueryBoResp=CommonUtil.objToObj(customerDetailQueryPo,CustomerDetailQueryBoResp.class);
            customerDetailQueryBoResp.setCustomerTypeDict(DictUtils.getDictLabel("custom_type",String.valueOf(customerDetailQueryPo.getCustomerType())));
            customerDetailQueryBoResp.setRewardFlagDict(DictUtils.getDictLabel("reward_flag",String.valueOf(customerDetailQueryPo.getRewardFlag())));
            customerDetailQueryBoResp.setAgentGradeDict(DictUtils.getDictLabel("agent_grade",String.valueOf(customerDetailQueryPo.getAgentGrade())));
            customerDetailQueryBoResp.setParentAgentGradeDict(DictUtils.getDictLabel("agent_grade",String.valueOf(customerDetailQueryPo.getParentAgentGrade())));
            return customerDetailQueryBoResp;
        }else{
            return null;
        }
    }

    @Override
    public CustomerDetailMobileQueryBoResp customerDetailMobileQuery() {
        CustomerDetailMobileQueryMoReq  customerDetailMobileQueryMoReq=new CustomerDetailMobileQueryMoReq();
        customerDetailMobileQueryMoReq.setDeleteFlag(1l);
        customerDetailMobileQueryMoReq.setCustomerPhone(SecurityUtils.getUsername());
        List<CustomerDetailMobileQueryPo> customerDetailMobileQueryPos=this.tCustomerMapper.customerDetailMobileQuery(customerDetailMobileQueryMoReq);
        if(StringUtils.isNotNull(customerDetailMobileQueryPos)&&customerDetailMobileQueryPos.size()>0){
            CustomerDetailMobileQueryPo customerDetailMobileQueryPo=customerDetailMobileQueryPos.get(0);
            CustomerDetailMobileQueryBoResp customerDetailMobileQueryBoResp=CommonUtil.objToObj(customerDetailMobileQueryPo,CustomerDetailMobileQueryBoResp.class);
            customerDetailMobileQueryBoResp.setCustomerTypeDict(DictUtils.getDictLabel("custom_type",String.valueOf(customerDetailMobileQueryPo.getCustomerType())));
            customerDetailMobileQueryBoResp.setAgentGradeDict(DictUtils.getDictLabel("agent_grade",String.valueOf(customerDetailMobileQueryPo.getAgentGrade())));
            //临时关闭
//            if(customerDetailMobileQueryBoResp.getRewardFlag()==0) {
//                HashMap<String,Object> map2=new HashMap<String,Object>();
//                map2.put("deleteFlag",1l);
//                map2.put("customerUuid",customerDetailMobileQueryPo.getCustomerUuid());
//                map2.put("rechargeType","first_pay");
//                BigDecimal  rechargeMoney=tRechargeRecordMapper.queryFirstRechargeRecord(map2);
//                if(StringUtils.isNotNull(rechargeMoney)&&rechargeMoney.compareTo(new BigDecimal(100.00))>=0){
//                    customerDetailMobileQueryBoResp.setRewardFlag(1l);
//                }
//            }
//            customerDetailMobileQueryBoResp.setRewardFlagDict(DictUtils.getDictLabel("reward_flag",String.valueOf(customerDetailMobileQueryBoResp.getRewardFlag())));
            HashMap<String,Object> map2=new HashMap<String,Object>();
            map2.put("deleteFlag",1l);
            map2.put("customerUuid",customerDetailMobileQueryPo.getCustomerUuid());

            Integer  rechargeRecordCount=tRechargeRecordMapper.judgeCustomerRechargeRecord(map2);
            if(StringUtils.isNull(rechargeRecordCount)||rechargeRecordCount==0) {
                customerDetailMobileQueryBoResp.setFirstRechargeRecord(true);
            }else{
                customerDetailMobileQueryBoResp.setFirstRechargeRecord(false);
            }
            return customerDetailMobileQueryBoResp;
        }else{
            return null;
        }
    }

    @Override
    public CustomerSignBoResq judgeCustomerSign() {
        CustomerSignQueryMoReq  customerSignQueryMoReq=new CustomerSignQueryMoReq();
        customerSignQueryMoReq.setDeleteFlag(1l);
        customerSignQueryMoReq.setCustomerPhone(SecurityUtils.getUsername());
        List<CustomerSignQueryPo> customerSignQueryPos=this.tCustomerMapper.customerSignQuery(customerSignQueryMoReq);

        if(StringUtils.isNotNull(customerSignQueryPos)&&customerSignQueryPos.size()>0){
            CustomerSignBoResq customerSignBoResq=new CustomerSignBoResq();
            CustomerSignQueryPo customerSignQueryPo=customerSignQueryPos.get(0);
            customerSignBoResq.setSignFileMainUuid(customerSignQueryPo.getSignFileMainUuid());
            customerSignBoResq.setSignFlag(StringUtils.isNotEmpty(customerSignQueryPo.getSignFileMainUuid()) ? new Boolean(true):new Boolean(false));
            return customerSignBoResq;
        }else{
            return null;
        }
    }

    @Override
    @Transactional
    public String uploadCustomerSign(UploadCustomerSignBoReq uploadCustomerSignBoReq) throws IOException {
        TCustomer tCustomer=this.tCustomerMapper.selectOne(new LambdaQueryWrapper<TCustomer>().eq(TCustomer::getCustomerPhone,SecurityUtils.getUsername()).eq(TCustomer::getDeleteFlag,1l));
        String signFileMainUuid = sysFileMainService.saveSysFile(uploadCustomerSignBoReq.getFile());
        if(StringUtils.isNotNull(tCustomer)&&StringUtils.isNotEmpty(signFileMainUuid)) {
            tCustomer.setSignFileMainUuid(signFileMainUuid);
            tCustomer.setRealName(uploadCustomerSignBoReq.getRealName());
            tCustomer.setCardNo(uploadCustomerSignBoReq.getCarNo());
            this.tCustomerMapper.updateById(tCustomer);
            return signFileMainUuid;
        }else{
            return null;
        }

    }

    @Override
    public CustomerBalanceConsumeBoResp balanceConsume(CustomerBalanceConsumeBoReq customerBalanceConsumeBoReq) {

        CustomerBalanceConsumeBoResp customerBalanceConsumeBoResp=new CustomerBalanceConsumeBoResp();
        BigDecimal zeroBigDecimal=  new BigDecimal(0);
           //订单超期校验
//        TOrder  tOrder= itOrderService.getOne(new LambdaQueryWrapper<TOrder>().eq(TOrder::getOrderCode,customerBalanceConsumeBoReq.getOrderCode()).eq(TOrder::getDeleteFlag,1l));
//        if(StringUtils.isNotNull(tOrder)) {
//            if("limitTime".equals(tOrder.getOrderStatus())) {
//                customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.ORDERLIMITTIME.getCode());
//                customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.ORDERLIMITTIME.getMsg());
//                customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
//                return customerBalanceConsumeBoResp;
//            }
//            else if(DateUtil.betweenMs(tOrder.getOrderFailureDatetime(),new Date())<=0) {
//                customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.ORDERLIMITTIME.getCode());
//                customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.ORDERLIMITTIME.getMsg());
//                customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
//                UcarOrderUpdateStatusBoReq ucarOrderUpdateStatusBoReq=new UcarOrderUpdateStatusBoReq();
//                ucarOrderUpdateStatusBoReq.setOrderStatus("limitTime");
//                ucarOrderUpdateStatusBoReq.setOrderCode(customerBalanceConsumeBoReq.getOrderCode());
//                itOrderService.updateOrderStatus(ucarOrderUpdateStatusBoReq);
//                return customerBalanceConsumeBoResp;
//            }
//        }

        TOrderPayDetail tOrderPayDetail=itOrderPayDetailService.getBaseMapper().selectOne(new LambdaQueryWrapper<TOrderPayDetail>().eq(TOrderPayDetail::getOrderCode,customerBalanceConsumeBoReq.getOrderCode()).eq(TOrderPayDetail::getDeleteFlag,1l));
        if(StringUtils.isNull(tOrderPayDetail)){
            tOrderPayDetail=new TOrderPayDetail();
            tOrderPayDetail.setOrderCode(customerBalanceConsumeBoReq.getOrderCode());
            tOrderPayDetail.setCreatedBy(SecurityUtils.getUsername());
        }
        TCustomer tCustomer=tCustomerMapper.selectOne(new LambdaQueryWrapper<TCustomer>()
                .eq(TCustomer::getSysUserId,SecurityUtils.getLoginUser()
                        .getUserId()).eq(TCustomer::getDeleteFlag,1L));
        if(tCustomer.getBalanceNumber().compareTo(zeroBigDecimal)==0&&tCustomer.getGiftMoney().compareTo(zeroBigDecimal)==0&&tCustomer.getReturnMoney().compareTo(zeroBigDecimal)==0){
            customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.CUSTOMERNOMONEY.getCode());
            customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.CUSTOMERNOMONEY.getMsg());
            customerBalanceConsumeBoResp.setUnpaidAmount(customerBalanceConsumeBoReq.getTotalFeel());
            tOrderPayDetail.setWxPay(customerBalanceConsumeBoReq.getTotalFeel());
            tOrderPayDetail.setGiftMoneyPay(zeroBigDecimal);
            tOrderPayDetail.setBalancePay(zeroBigDecimal);
            tOrderPayDetail.setReturnMoneyPay(zeroBigDecimal);
        }else{
            if(tCustomer.getBalanceNumber().compareTo(customerBalanceConsumeBoReq.getTotalFeel())>=0){
                tCustomer.setBalanceNumber(tCustomer.getBalanceNumber().subtract(customerBalanceConsumeBoReq.getTotalFeel()));
                customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.SUCCESS.getCode());
                customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.SUCCESS.getMsg());
                customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
                tOrderPayDetail.setBalancePay(customerBalanceConsumeBoReq.getTotalFeel());
                tOrderPayDetail.setGiftMoneyPay(zeroBigDecimal);
                tOrderPayDetail.setWxPay(zeroBigDecimal);
                tOrderPayDetail.setReturnMoneyPay(zeroBigDecimal);
                tCustomerMapper.updateById(tCustomer);
            }else{
                if(tCustomer.getBalanceNumber().compareTo(zeroBigDecimal)>0){
                    tOrderPayDetail.setBalancePay(tCustomer.getBalanceNumber());
                    BigDecimal unpaidAmount=customerBalanceConsumeBoReq.getTotalFeel().subtract(tCustomer.getBalanceNumber());
                    tCustomer.setBalanceNumber(zeroBigDecimal);
                    HashMap<String,Object> map2=new HashMap<>();
                    map2.put("orderCode",tCustomer.getCustomerUuid());
                    map2.put("orderStatus","vaild");
                    BigDecimal sumPay=tOrderPayDetailMapper.queryCustomerSumPay(map2);
                    if(StringUtils.isNotNull(sumPay)&&sumPay.compareTo(new BigDecimal(100.00))>=0) {
                        if (tCustomer.getGiftMoney().compareTo(unpaidAmount) >= 0) {
                            tOrderPayDetail.setGiftMoneyPay(unpaidAmount);
                            tOrderPayDetail.setWxPay(zeroBigDecimal);
                            tOrderPayDetail.setReturnMoneyPay(zeroBigDecimal);
                            tCustomer.setGiftMoney(tCustomer.getGiftMoney().subtract(unpaidAmount));
                            customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.SUCCESS.getCode());
                            customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.SUCCESS.getMsg());
                            customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
                            tCustomerMapper.updateById(tCustomer);
                        } else {
                            tOrderPayDetail.setGiftMoneyPay(tCustomer.getGiftMoney());
                            tCustomer.setGiftMoney(zeroBigDecimal);
                            BigDecimal unpaidAmount2 = unpaidAmount.subtract(tCustomer.getGiftMoney());
                            if (tCustomer.getReturnMoney().compareTo(unpaidAmount2) >= 0) {
                                tOrderPayDetail.setReturnMoneyPay(unpaidAmount2);
                                tOrderPayDetail.setWxPay(zeroBigDecimal);
                                tCustomer.setReturnMoney(tCustomer.getReturnMoney().subtract(unpaidAmount2));
                                customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.SUCCESS.getCode());
                                customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.SUCCESS.getMsg());
                                customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
                                tCustomerMapper.updateById(tCustomer);
                            } else {
                                BigDecimal unpaidAmount3 = unpaidAmount2.subtract(tCustomer.getReturnMoney());
                                tOrderPayDetail.setReturnMoneyPay(tCustomer.getReturnMoney());
                                tCustomer.setReturnMoney(zeroBigDecimal);
                                customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.CUSTOMERMONEYNOTENOUGH.getCode());
                                customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.CUSTOMERMONEYNOTENOUGH.getMsg());
                                customerBalanceConsumeBoResp.setUnpaidAmount(unpaidAmount3);
                                tOrderPayDetail.setWxPay(unpaidAmount3);
                            }
                        }
                    }else{
                        tOrderPayDetail.setGiftMoneyPay(zeroBigDecimal);
                        if(tCustomer.getReturnMoney().compareTo(unpaidAmount)>=0){
                            tOrderPayDetail.setReturnMoneyPay(unpaidAmount);
                            tOrderPayDetail.setWxPay(zeroBigDecimal);
                            tCustomer.setReturnMoney(tCustomer.getReturnMoney().subtract(unpaidAmount));
                            customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.SUCCESS.getCode());
                            customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.SUCCESS.getMsg());
                            customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
                            tCustomerMapper.updateById(tCustomer);
                        }else{
                            BigDecimal unpaidAmount2=unpaidAmount.subtract(tCustomer.getReturnMoney());
                            tOrderPayDetail.setReturnMoneyPay(tCustomer.getReturnMoney());
                            tCustomer.setReturnMoney(zeroBigDecimal);
                            customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.CUSTOMERMONEYNOTENOUGH.getCode());
                            customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.CUSTOMERMONEYNOTENOUGH.getMsg());
                            customerBalanceConsumeBoResp.setUnpaidAmount(unpaidAmount2);
                            tOrderPayDetail.setWxPay(unpaidAmount2);
                        }
                    }
                }else{
                    tOrderPayDetail.setBalancePay(zeroBigDecimal);
                    HashMap<String,Object> map2=new HashMap<>();
                    map2.put("orderCode",tCustomer.getCustomerUuid());
                    map2.put("orderStatus","vaild");
                    BigDecimal sumPay=tOrderPayDetailMapper.queryCustomerSumPay(map2);
                    if(StringUtils.isNotNull(sumPay)&&sumPay.compareTo(new BigDecimal(100.00))>=0) {
                        if (tCustomer.getGiftMoney().compareTo(customerBalanceConsumeBoReq.getTotalFeel()) >= 0) {
                            tOrderPayDetail.setGiftMoneyPay(customerBalanceConsumeBoReq.getTotalFeel());
                            tOrderPayDetail.setWxPay(zeroBigDecimal);
                            tOrderPayDetail.setReturnMoneyPay(zeroBigDecimal);
                            tCustomer.setGiftMoney(tCustomer.getGiftMoney().subtract(customerBalanceConsumeBoReq.getTotalFeel()));
                            customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.SUCCESS.getCode());
                            customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.SUCCESS.getMsg());
                            customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
                            tCustomerMapper.updateById(tCustomer);
                        } else {
                            tOrderPayDetail.setGiftMoneyPay(tCustomer.getGiftMoney());
                            tCustomer.setGiftMoney(zeroBigDecimal);
                            BigDecimal unpaidAmount2 = customerBalanceConsumeBoReq.getTotalFeel().subtract(tCustomer.getGiftMoney());
                            if (tCustomer.getReturnMoney().compareTo(unpaidAmount2) >= 0) {
                                tOrderPayDetail.setReturnMoneyPay(unpaidAmount2);
                                tOrderPayDetail.setWxPay(zeroBigDecimal);
                                tCustomer.setReturnMoney(tCustomer.getReturnMoney().subtract(unpaidAmount2));
                                customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.SUCCESS.getCode());
                                customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.SUCCESS.getMsg());
                                customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
                                tCustomerMapper.updateById(tCustomer);
                            } else {
                                BigDecimal unpaidAmount3 = unpaidAmount2.subtract(tCustomer.getReturnMoney());
                                tOrderPayDetail.setReturnMoneyPay(tCustomer.getReturnMoney());
                                tCustomer.setReturnMoney(zeroBigDecimal);
                                customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.CUSTOMERMONEYNOTENOUGH.getCode());
                                customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.CUSTOMERMONEYNOTENOUGH.getMsg());
                                customerBalanceConsumeBoResp.setUnpaidAmount(unpaidAmount3);
                                tOrderPayDetail.setWxPay(unpaidAmount3);
                            }
                        }
                    }else{
                        tOrderPayDetail.setGiftMoneyPay(zeroBigDecimal);
                        if(tCustomer.getReturnMoney().compareTo(customerBalanceConsumeBoReq.getTotalFeel())>=0){
                            tOrderPayDetail.setReturnMoneyPay(customerBalanceConsumeBoReq.getTotalFeel());
                            tOrderPayDetail.setWxPay(zeroBigDecimal);
                            tCustomer.setReturnMoney(tCustomer.getReturnMoney().subtract(customerBalanceConsumeBoReq.getTotalFeel()));
                            customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.SUCCESS.getCode());
                            customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.SUCCESS.getMsg());
                            customerBalanceConsumeBoResp.setUnpaidAmount(zeroBigDecimal);
                            tCustomerMapper.updateById(tCustomer);
                        }else{
                            BigDecimal unpaidAmount2=customerBalanceConsumeBoReq.getTotalFeel().subtract(tCustomer.getReturnMoney());
                            tOrderPayDetail.setReturnMoneyPay(tCustomer.getReturnMoney());
                            tCustomer.setReturnMoney(zeroBigDecimal);
                            customerBalanceConsumeBoResp.setConsumeCode(ResultEnum.CUSTOMERMONEYNOTENOUGH.getCode());
                            customerBalanceConsumeBoResp.setConsumeMessage(ResultEnum.CUSTOMERMONEYNOTENOUGH.getMsg());
                            customerBalanceConsumeBoResp.setUnpaidAmount(unpaidAmount2);
                            tOrderPayDetail.setWxPay(unpaidAmount2);
                        }
                    }
                }
            }
            //更新订单状态
            if(tOrderPayDetail.getWxPay().compareTo(zeroBigDecimal)==0){
                UcarOrderUpdateStatusBoReq ucarOrderUpdateStatusBoReq=new UcarOrderUpdateStatusBoReq();
                ucarOrderUpdateStatusBoReq.setPayStatus("paid");
                ucarOrderUpdateStatusBoReq.setOrderStatus("waitVaild");
                ucarOrderUpdateStatusBoReq.setOrderCode(customerBalanceConsumeBoReq.getOrderCode());
                ucarOrderUpdateStatusBoReq.setPayDatetime(LocalDateTimeUtil.now());
                itOrderService.updateOrderStatus(ucarOrderUpdateStatusBoReq,null);
            }else{
                UcarOrderUpdateStatusBoReq ucarOrderUpdateStatusBoReq=new UcarOrderUpdateStatusBoReq();
                ucarOrderUpdateStatusBoReq.setPayStatus("paying");
                ucarOrderUpdateStatusBoReq.setOrderCode(customerBalanceConsumeBoReq.getOrderCode());
                itOrderService.updateOrderStatus(ucarOrderUpdateStatusBoReq,null);
            }
        }
        itOrderPayDetailService.saveOrUpdate(tOrderPayDetail);
        return customerBalanceConsumeBoResp;
    }

    @Override
    @Transactional
    public CustomerBalanceRestoreBoResp balanceRestore(CustomerBalanceRestoreBoReq customerBalanceRestoreBoReq) {
        TOrderPayDetail tOrderPayDetail=itOrderPayDetailService.getBaseMapper().selectOne(new LambdaQueryWrapper<TOrderPayDetail>().eq(TOrderPayDetail::getOrderCode,customerBalanceRestoreBoReq.getOrderCode()).eq(TOrderPayDetail::getDeleteFlag,1l));

        HashMap<String,Object> map2=new HashMap<>();
        map2.put("orderCode",customerBalanceRestoreBoReq.getOrderCode());
        map2.put("deleteFlag",1l);
        TCustomer tCustomer= tCustomerMapper.selectCustomerByOrderCode(map2);

        CustomerBalanceRestoreBoResp customerBalanceRestoreBoResp=new CustomerBalanceRestoreBoResp();
        BigDecimal zeroBigDecimal=  new BigDecimal(0);
        if(tOrderPayDetail.getWxPay().compareTo(zeroBigDecimal)==0) {
            if (tOrderPayDetail.getBalancePay().compareTo(zeroBigDecimal) > 0) {
                tCustomer.setBalanceNumber(tCustomer.getBalanceNumber().add(tOrderPayDetail.getBalancePay()));
            }
            if (tOrderPayDetail.getGiftMoneyPay().compareTo(zeroBigDecimal) > 0) {
                tCustomer.setGiftMoney(tCustomer.getGiftMoney().add(tOrderPayDetail.getGiftMoneyPay()));
            }
            //退单的话，返现金额失效
            if (tOrderPayDetail.getReturnMoneyPay().compareTo(zeroBigDecimal) > 0) {
                tCustomer.setReturnMoney(tCustomer.getReturnMoney().add(tOrderPayDetail.getReturnMoneyPay()));
            }
//              List<TOrder> orderList=itOrderService.list(new LambdaQueryWrapper<TOrder>().eq(TOrder::getCustomerUuidMapping,tCustomer.getCustomerUuid()).eq(TOrder::getDeleteFlag,1l).eq(TOrder::getIsReturnPrice,2));
//              if(StringUtils.isNotNull(orderList)&&orderList.size()>0) {
//                BigDecimal sumReturnPrice = orderList.stream()
//                      .filter(obj -> obj.getTotalReturnPrice() != null)
//                      .map(TOrder::getTotalReturnPrice)
//                      .reduce(BigDecimal.ZERO, BigDecimal::add);
//              }
            UcarOrderUpdateStatusBoReq ucarOrderUpdateStatusBoReq=new UcarOrderUpdateStatusBoReq();
            ucarOrderUpdateStatusBoReq.setPayStatus("payInvalid");
            ucarOrderUpdateStatusBoReq.setOrderStatus("invalid");
            ucarOrderUpdateStatusBoReq.setChargeDatetime(LocalDateTimeUtil.now());
            ucarOrderUpdateStatusBoReq.setOrderCode(customerBalanceRestoreBoReq.getOrderCode());
            itOrderService.updateOrderStatus(ucarOrderUpdateStatusBoReq,tCustomer);
            customerBalanceRestoreBoResp.setRestoreCode(ResultEnum.SUCCESS.getCode());
            customerBalanceRestoreBoResp.setRestoreMessage(ResultEnum.SUCCESS.getMsg());
            customerBalanceRestoreBoResp.setUnpaidAmount(zeroBigDecimal);
        }else{
            customerBalanceRestoreBoResp.setRestoreCode(ResultEnum.ORDERBACKWXPAY.getCode());
            customerBalanceRestoreBoResp.setRestoreMessage(ResultEnum.ORDERBACKWXPAY.getMsg());
            customerBalanceRestoreBoResp.setUnpaidAmount(tOrderPayDetail.getWxPay());
        }
        return customerBalanceRestoreBoResp;
    }



    @Override
    public List<InComeTotolBoResp> income(InComeTotolBoReq inComeTotolBoReq) {
        TCustomer tCustomer= tCustomerMapper.selectOne(new LambdaQueryWrapper<TCustomer>()
                .eq(TCustomer::getSysUserId,SecurityUtils.getLoginUser()
                        .getUserId()).eq(TCustomer::getDeleteFlag,1L));
        List<InComeTotolBoResp> inComeTotolBoRespList=new ArrayList<>();
        List<TOrder> tOrderList= itOrderService.list(new LambdaQueryWrapper<TOrder>().eq(TOrder::getCustomerUuidMapping,tCustomer.getCustomerUuid()).eq(TOrder::getDeleteFlag,1l).isNotNull(TOrder::getPayDatetime));
        BigDecimal zeroBigDecimal=  new BigDecimal(0);
        if("all".equals(inComeTotolBoReq.getClassify())){
            if(tOrderList!=null&&tOrderList.size()>0){
                tOrderList.forEach(tOrder -> {
                    if(tOrder.getTotalReturnPrice().compareTo(zeroBigDecimal)>0&&tOrder.getIsReturnPrice()==2){
                        InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                        inComeTotolBoResp.setAmount(tOrder.getTotalReturnPrice());
                        inComeTotolBoResp.setInComeTypeDict("返现奖励红包收入");
                        inComeTotolBoResp.setInComeType("redEnvelope");
                        inComeTotolBoResp.setDate(tOrder.getPayDatetime());
                        inComeTotolBoResp.setBalanceNumber(tCustomer.getBalanceNumber());
                        inComeTotolBoResp.setDataUUid(tOrder.getOrderUuid());
                        inComeTotolBoRespList.add(inComeTotolBoResp);
                    }
                    if(StringUtils.isNotNull(tOrder.getAgencyFee())&&tOrder.getAgencyFee().compareTo(zeroBigDecimal)>0&&"agentCus".equals(tCustomer.getCustomerType())){
                        InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                        inComeTotolBoResp.setAmount(tOrder.getAgencyFee());
                        inComeTotolBoResp.setInComeTypeDict("代理商代理费收入");
                        inComeTotolBoResp.setInComeType("agencyFee");
                        inComeTotolBoResp.setBalanceNumber(tCustomer.getBalanceNumber());
                        inComeTotolBoResp.setDate(tOrder.getPayDatetime());
                        inComeTotolBoResp.setDataUUid(tOrder.getOrderUuid());
                        inComeTotolBoRespList.add(inComeTotolBoResp);
                    }
                        InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                        inComeTotolBoResp.setAmount(tOrder.getTotalRealPrice());
                        if(StringUtils.isNotEmpty(tOrder.getOrderStatus())&&"invalid".equals(tOrder.getOrderStatus())) {
                            inComeTotolBoResp.setInComeTypeDict("订单退单费用");
                            inComeTotolBoResp.setInComeType("orderRestore");
                            InComeTotolBoResp inComeTotolBoResp2=new InComeTotolBoResp();
                            inComeTotolBoResp2.setAmount(tOrder.getTotalRealPrice());
                            inComeTotolBoResp2.setInComeTypeDict("订单消费费用");
                            inComeTotolBoResp2.setInComeType("orderConsume");
                            inComeTotolBoResp2.setBalanceNumber(tCustomer.getBalanceNumber());
                            inComeTotolBoResp2.setDate(tOrder.getPayDatetime());
                            inComeTotolBoResp2.setDataUUid(tOrder.getOrderUuid());
                            inComeTotolBoRespList.add(inComeTotolBoResp2);

                        }else{
                            inComeTotolBoResp.setInComeTypeDict("订单消费费用");
                            inComeTotolBoResp.setInComeType("orderConsume");
                        }
                        inComeTotolBoResp.setBalanceNumber(tCustomer.getBalanceNumber());
                        inComeTotolBoResp.setDate(tOrder.getPayDatetime());
                        inComeTotolBoResp.setDataUUid(tOrder.getOrderUuid());
                        inComeTotolBoRespList.add(inComeTotolBoResp);
                });
            }
            List<TRechargeRecord>  rechargeRecords=  tRechargeRecordMapper.selectList(new LambdaQueryWrapper<TRechargeRecord>().eq(TRechargeRecord::getCustomerUuid,tCustomer.getCustomerUuid()).eq(TRechargeRecord::getDeleteFlag,1l));
            if(rechargeRecords!=null&&rechargeRecords.size()>0){
                rechargeRecords.forEach(rechargeRecord -> {
                    InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                    inComeTotolBoResp.setAmount(rechargeRecord.getRechargeMoney());
                    inComeTotolBoResp.setInComeTypeDict("充值收入");
                    inComeTotolBoResp.setInComeType("rechargeRecord");
                    inComeTotolBoResp.setDate(rechargeRecord.getRechargeDatetime());
                    inComeTotolBoResp.setDataUUid(rechargeRecord.getRechargeRecordUuid());
                    inComeTotolBoResp.setRechargeStatus(rechargeRecord.getRechargeStatus());
                    inComeTotolBoResp.setRechargeType(rechargeRecord.getRechargeType());
                    inComeTotolBoResp.setRechargeStatusDict(DictUtils.getDictLabel("recharge_status",rechargeRecord.getRechargeStatus()));
                    inComeTotolBoResp.setRechargeTypeDict(DictUtils.getDictLabel("recharge_type",rechargeRecord.getRechargeType()));
                    inComeTotolBoRespList.add(inComeTotolBoResp);
                    if(StringUtils.isNotEmpty(rechargeRecord.getRechargeType())&&"first_pay".equals(rechargeRecord.getRechargeType())&&rechargeRecord.getGiveMoney().compareTo(new BigDecimal(0.00))>0){
                        InComeTotolBoResp inComeTotolBoResp2=new InComeTotolBoResp();
                        inComeTotolBoResp2.setAmount(rechargeRecord.getGiveMoney());
                        inComeTotolBoResp2.setInComeTypeDict("充值赠送金额收入");
                        inComeTotolBoResp2.setInComeType("rechargeGitMoney");
                        inComeTotolBoResp2.setDate(rechargeRecord.getRechargeDatetime());
                        inComeTotolBoResp2.setDataUUid(rechargeRecord.getRechargeRecordUuid());
                        inComeTotolBoResp2.setRechargeStatus(rechargeRecord.getRechargeStatus());
                        inComeTotolBoResp2.setRechargeType(rechargeRecord.getRechargeType());
                        inComeTotolBoResp2.setRechargeStatusDict(DictUtils.getDictLabel("recharge_status",rechargeRecord.getRechargeStatus()));
                        inComeTotolBoResp2.setRechargeTypeDict(DictUtils.getDictLabel("recharge_type",rechargeRecord.getRechargeType()));
                        inComeTotolBoRespList.add(inComeTotolBoResp2);
                        List<TConsumeData> tConsumeDataList= tConsumeDataMapper.selectList(new LambdaQueryWrapper<TConsumeData>().eq(TConsumeData::getCustomerUuid,tCustomer.getCustomerUuid()).eq(TConsumeData::getConsumeType,"backGitMoney").eq(TConsumeData::getDeleteFlag,1l));
                        if(StringUtils.isNotNull(tConsumeDataList)&&tConsumeDataList.size()>0){
                            tConsumeDataList.forEach(tConsumeData -> {
                                InComeTotolBoResp inComeTotolBoResp3=new InComeTotolBoResp();
                                inComeTotolBoResp3.setAmount(tConsumeData.getConsumeMoney());
                                inComeTotolBoResp3.setInComeTypeDict("退回充值赠送金额收入");
                                inComeTotolBoResp3.setInComeType("backRechargeGitMoney");
                                inComeTotolBoResp3.setDate(tConsumeData.getConsumeDatetime());
                                inComeTotolBoRespList.add(inComeTotolBoResp3);
                            });
                        }
                    }
                });
            }
            List<TWithdrawRecord>  tWithdrawRecords=  tWithdrawRecordMapper.selectList(new LambdaQueryWrapper<TWithdrawRecord>().eq(TWithdrawRecord::getCustomerUuid,tCustomer.getCustomerUuid()).eq(TWithdrawRecord::getDeleteFlag,1l));
            if(tWithdrawRecords!=null&&tWithdrawRecords.size()>0){
                tWithdrawRecords.forEach(tWithdrawRecord -> {
                    InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                    inComeTotolBoResp.setAmount(tWithdrawRecord.getWithdrawMoney());
                    inComeTotolBoResp.setInComeTypeDict("用户提现消费费用");
                    inComeTotolBoResp.setInComeType("withdRaw");
                    inComeTotolBoResp.setDate(tWithdrawRecord.getWithdrawDatetime());
                    inComeTotolBoResp.setDataUUid(tWithdrawRecord.getWithdrawRecordUuid());
                    inComeTotolBoResp.setWithdrawWay(tWithdrawRecord.getWithdrawWay());
                    inComeTotolBoResp.setWithdrawWayDict(DictUtils.getDictLabel("withdraw_way",tWithdrawRecord.getWithdrawWay()));
                    inComeTotolBoRespList.add(inComeTotolBoResp);
                });
            }
            List<TConsumeData> tConsumeDataList= tConsumeDataMapper.selectList(new LambdaQueryWrapper<TConsumeData>().eq(TConsumeData::getCustomerUuid,tCustomer.getCustomerUuid()).eq(TConsumeData::getConsumeType,"backReturnMoney").eq(TConsumeData::getDeleteFlag,1l));
            if(StringUtils.isNotNull(tConsumeDataList)&&tConsumeDataList.size()>0){
                tConsumeDataList.forEach(tConsumeData -> {
                    InComeTotolBoResp inComeTotolBoResp3=new InComeTotolBoResp();
                    inComeTotolBoResp3.setAmount(tConsumeData.getConsumeMoney());
                    inComeTotolBoResp3.setInComeTypeDict("退回订单返现金额收入");
                    inComeTotolBoResp3.setInComeType("backOrderReturnMoney");
                    inComeTotolBoResp3.setDate(tConsumeData.getConsumeDatetime());
                    inComeTotolBoRespList.add(inComeTotolBoResp3);
                });
            }
        }
        else if("in".equals(inComeTotolBoReq.getClassify()))
        {
           if(tOrderList!=null&&tOrderList.size()>0){
               tOrderList.forEach(tOrder -> {
                   if(tOrder.getTotalReturnPrice().compareTo(zeroBigDecimal)>0&&tOrder.getIsReturnPrice()==2){
                       InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                       inComeTotolBoResp.setAmount(tOrder.getTotalReturnPrice());
                       inComeTotolBoResp.setInComeTypeDict("返现奖励红包收入");
                       inComeTotolBoResp.setInComeType("redEnvelope");
                       inComeTotolBoResp.setDate(tOrder.getPayDatetime());
                       inComeTotolBoResp.setDataUUid(tOrder.getOrderUuid());
                       inComeTotolBoRespList.add(inComeTotolBoResp);
                   }
                   if(StringUtils.isNotNull(tOrder.getAgencyFee())&&tOrder.getAgencyFee().compareTo(zeroBigDecimal)>0&&"agentCus".equals(tCustomer.getCustomerType())){
                       InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                       inComeTotolBoResp.setAmount(tOrder.getAgencyFee());
                       inComeTotolBoResp.setInComeTypeDict("代理商代理费收入");
                       inComeTotolBoResp.setInComeType("agencyFee");
                       inComeTotolBoResp.setDate(tOrder.getPayDatetime());
                       inComeTotolBoResp.setDataUUid(tOrder.getOrderUuid());
                       inComeTotolBoRespList.add(inComeTotolBoResp);
                   }
                           if(StringUtils.isNotEmpty(tOrder.getOrderStatus())&&"invalid".equals(tOrder.getOrderStatus())) {
                               InComeTotolBoResp inComeTotolBoResp = new InComeTotolBoResp();
                               inComeTotolBoResp.setAmount(tOrder.getTotalRealPrice());
                               inComeTotolBoResp.setInComeTypeDict("订单退单费用");
                               inComeTotolBoResp.setInComeType("orderRestore");
                               inComeTotolBoResp.setBalanceNumber(tCustomer.getBalanceNumber());
                               inComeTotolBoResp.setDate(tOrder.getPayDatetime());
                               inComeTotolBoResp.setDataUUid(tOrder.getOrderUuid());
                               inComeTotolBoRespList.add(inComeTotolBoResp);
                           }
               });
           }
             List<TRechargeRecord>  rechargeRecords=  tRechargeRecordMapper.selectList(new LambdaQueryWrapper<TRechargeRecord>().eq(TRechargeRecord::getCustomerUuid,tCustomer.getCustomerUuid()).eq(TRechargeRecord::getDeleteFlag,1l));
             if(rechargeRecords!=null&&rechargeRecords.size()>0){
                rechargeRecords.forEach(rechargeRecord -> {
                    InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                    inComeTotolBoResp.setAmount(rechargeRecord.getRechargeMoney());
                    inComeTotolBoResp.setInComeTypeDict("充值收入");
                    inComeTotolBoResp.setInComeType("rechargeRecord");
                    inComeTotolBoResp.setDate(rechargeRecord.getRechargeDatetime());
                    inComeTotolBoResp.setDataUUid(rechargeRecord.getRechargeRecordUuid());
                    inComeTotolBoRespList.add(inComeTotolBoResp);
                    if(StringUtils.isNotEmpty(rechargeRecord.getRechargeType())&&"first_pay".equals(rechargeRecord.getRechargeType())&&rechargeRecord.getGiveMoney().compareTo(new BigDecimal(0.00))>0){
                        InComeTotolBoResp inComeTotolBoResp2=new InComeTotolBoResp();
                        inComeTotolBoResp2.setAmount(rechargeRecord.getGiveMoney());
                        inComeTotolBoResp2.setInComeTypeDict("充值赠送金额收入");
                        inComeTotolBoResp2.setInComeType("rechargeGitMoney");
                        inComeTotolBoResp2.setDate(rechargeRecord.getRechargeDatetime());
                        inComeTotolBoResp2.setDataUUid(rechargeRecord.getRechargeRecordUuid());
                        inComeTotolBoResp2.setRechargeStatus(rechargeRecord.getRechargeStatus());
                        inComeTotolBoResp2.setRechargeType(rechargeRecord.getRechargeType());
                        inComeTotolBoResp2.setRechargeStatusDict(DictUtils.getDictLabel("recharge_status",rechargeRecord.getRechargeStatus()));
                        inComeTotolBoResp2.setRechargeTypeDict(DictUtils.getDictLabel("recharge_type",rechargeRecord.getRechargeType()));
                        inComeTotolBoRespList.add(inComeTotolBoResp2);
                    }
                });
              }
        }
        else if("come".equals(inComeTotolBoReq.getClassify()))
        {
            if(tOrderList!=null&&tOrderList.size()>0){
                tOrderList.forEach(tOrder -> {
                            if(StringUtils.isNotEmpty(tOrder.getOrderStatus())) {
                                InComeTotolBoResp inComeTotolBoResp = new InComeTotolBoResp();
                                inComeTotolBoResp.setAmount(tOrder.getTotalRealPrice());
                                inComeTotolBoResp.setInComeTypeDict("订单消费费用");
                                inComeTotolBoResp.setInComeType("orderConsume");
                                inComeTotolBoResp.setDate(tOrder.getPayDatetime());
                                inComeTotolBoResp.setDataUUid(tOrder.getOrderUuid());
                                inComeTotolBoRespList.add(inComeTotolBoResp);
                            }
                });
            }
            List<TWithdrawRecord>  tWithdrawRecords=  tWithdrawRecordMapper.selectList(new LambdaQueryWrapper<TWithdrawRecord>().eq(TWithdrawRecord::getCustomerUuid,tCustomer.getCustomerUuid()).eq(TWithdrawRecord::getDeleteFlag,1l));
            if(tWithdrawRecords!=null&&tWithdrawRecords.size()>0){
                tWithdrawRecords.forEach(tWithdrawRecord -> {
                    InComeTotolBoResp inComeTotolBoResp=new InComeTotolBoResp();
                    inComeTotolBoResp.setAmount(tWithdrawRecord.getWithdrawMoney());
                    inComeTotolBoResp.setInComeTypeDict("用户提现消费费用");
                    inComeTotolBoResp.setInComeType("withdRaw");
                    inComeTotolBoResp.setDate(tWithdrawRecord.getWithdrawDatetime());
                    inComeTotolBoResp.setDataUUid(tWithdrawRecord.getWithdrawRecordUuid());
                    inComeTotolBoResp.setWithdrawWay(tWithdrawRecord.getWithdrawWay());
                    inComeTotolBoResp.setWithdrawWayDict(DictUtils.getDictLabel("withdraw_way",tWithdrawRecord.getWithdrawWay()));
                    inComeTotolBoRespList.add(inComeTotolBoResp);
                });
            }
            List<TConsumeData> tConsumeDataList= tConsumeDataMapper.selectList(new LambdaQueryWrapper<TConsumeData>().eq(TConsumeData::getCustomerUuid,tCustomer.getCustomerUuid()).eq(TConsumeData::getDeleteFlag,1l));
            if(StringUtils.isNotNull(tConsumeDataList)&&tConsumeDataList.size()>0){
                tConsumeDataList.forEach(tConsumeData -> {
                    InComeTotolBoResp inComeTotolBoResp3=new InComeTotolBoResp();
                    inComeTotolBoResp3.setAmount(tConsumeData.getConsumeMoney());
                    if(StringUtils.isNotEmpty(tConsumeData.getConsumeType())&&"backGitMoney".equals(tConsumeData.getConsumeType())) {
                        inComeTotolBoResp3.setInComeTypeDict("退回充值赠送金额收入");
                        inComeTotolBoResp3.setInComeType("backRechargeGitMoney");
                    }else if(StringUtils.isNotEmpty(tConsumeData.getConsumeType())&&"backReturnMoney".equals(tConsumeData.getConsumeType())) {
                        inComeTotolBoResp3.setInComeTypeDict("退回订单返现金额收入");
                        inComeTotolBoResp3.setInComeType("backOrderReturnMoney");
                    }
                    inComeTotolBoResp3.setDate(tConsumeData.getConsumeDatetime());
                    inComeTotolBoRespList.add(inComeTotolBoResp3);
                });
            }
        }
        return inComeTotolBoRespList;
    }



    @Override
    public int uploadCustomerData(CustomerUploadDataBoReq customerUploadDataBoReq) {
        TCustomer tCustomer= tCustomerMapper.selectOne(new LambdaQueryWrapper<TCustomer>()
                .eq(TCustomer::getSysUserId,SecurityUtils.getLoginUser()
                        .getUserId()).eq(TCustomer::getDeleteFlag,1L));
        InputStream inputStream=null;
        try {
//            if (StringUtils.isNotEmpty(customerUploadDataBoReq.getAvatarUrl())) {
//                HttpResponse res = HttpUtil.createGet(customerUploadDataBoReq.getAvatarUrl())
//                        .timeout(5000) // 5秒超时
//                        .execute();
//                if (res.isOk()) {
//                      inputStream = res.bodyStream();
//                    String fileNewName= UUID.randomUUID().toString();
//                    String ext = FileNameUtil.extName(customerUploadDataBoReq.getAvatarUrl());
                    SysFileData  sysFileData=sysFileMainService.saveSysFile(customerUploadDataBoReq.getAvatarFile(), AccessControlList.REST_CANNED_PUBLIC_READ);
                    tCustomer.setAvatarUrl(sysFileData.getFileUrl());
//                }
//            }
            if (StringUtils.isNotEmpty(customerUploadDataBoReq.getNickName())) {
                tCustomer.setCustomerName(customerUploadDataBoReq.getNickName());
            }
        } catch (HttpException e) {
            logger.error("上传头像错误：{}",e.getLocalizedMessage());
        } catch (IOException e) {
            logger.error("上传头像错误：{}",e.getLocalizedMessage());
        } finally {
            if(StringUtils.isNotNull(inputStream)){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error("上传头像错误：{}",e.getLocalizedMessage());
                }
            }
        }
        return tCustomerMapper.updateById(tCustomer);
    }
}
